<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
			<el-form-item label="申请编号" prop="desNo">
				<el-input v-model="queryParams.desNo" placeholder="请输入申请编号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案任务名称" prop="desName">
				<el-input v-model="queryParams.desName" placeholder="请输入档案任务名称" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="销毁状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择销毁状态" clearable>
					<el-option v-for="dict in destroyed_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<!-- <el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['archive:ArchiveDesTask:export']">导出</el-button>
			</el-col> -->
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="ArchiveDesTaskList" @selection-change="handleSelectionChange">
			<!-- <el-table-column type="selection" width="55" align="center" /> -->
			<el-table-column label="序号" align='center' width="50">
				<template #default="scope">
					{{ scope.$index+1 }}
				</template>
			</el-table-column>
			<el-table-column label="申请编号" align="center" prop="desNo" />
			<el-table-column label="销毁状态" align="center" prop="status">
				<template #default="scope">
					<dict-tag :options="destroyed_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="档案任务名称" align="center" prop="desName" />
			<el-table-column label="陪同人员" align="center" prop="desAccomBy" />
			<el-table-column label="任务内容" align="center" prop="desContent" />
			<el-table-column label="操作" align="center" fixed="right" width="300">
				<template #default="scope">
					<el-button type="text" icon="View" @click="handleView(scope.row, scope.index)" v-hasPermi="['archive:ArchiveDesTask:query']">详细</el-button>

				</template>
			</el-table-column>
		</el-table>
		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
		 @pagination="getList" />
		<!-- 查看详情 -->
		<el-dialog title="销毁任务详细" v-model="openscan" width="90%" append-to-body>
			<el-form ref="ArchiveDesTaskRef" :model="form" label-width="170px">
				<el-row>
					<el-col :span="6">
						<el-form-item label="申请编号" prop="desNo">
							<el-input v-model="form.desNo" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="档案任务名称" prop="desName">
							<el-input v-model="form.desName" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="陪同人员" prop="desAccomBy">
							<el-input v-model="form.desAccomBy" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="任务内容" prop="desContent">
							<el-input v-model="form.desContent" disabled="disabled" />
						</el-form-item>
					</el-col>
					<!-- <el-col :span="6">
						<el-form-item label="推送状态" prop="pushStatus">
							<el-select v-model="form.pushStatus" disabled="disabled">
								<el-option v-for="dict in push_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col> -->
					<el-col :span="6">
						<el-form-item label="销毁状态" prop="status">
							<el-select v-model="form.status" disabled="disabled">
								<el-option v-for="dict in destroyed_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<el-divider content-position="center">销毁档案详情</el-divider>
			<el-row style="margin-bottom: 10px; ">
				<el-button type="danger" @click="destroyArchivesList">销毁</el-button>
			</el-row>
			<el-table :data="archiveDestoryList" ref="destructionFiles" @selection-change="handleSelectionChangeArchives" style="min-height: 450px;border:1px solid #dcdfe6;">
				<el-table-column type="selection" width="55">
				</el-table-column>
				<el-table-column label="序号" align='center' width="50">
					<template #default="scope">
						{{ scope.$index+1 }}
					</template>
				</el-table-column>
				<el-table-column label="档案区编号" width="150" align="center" prop="areaNo" />
				<el-table-column label="档案柜编号" align="center" prop="cabinetNo" width="150" />
				<el-table-column label="档案盒号" align="center" prop="boxNo" width="150" />
				<el-table-column label="档案盒位置" align="center" prop="storLoc" width="300" />
				<el-table-column label="档案名称" align="center" prop="archiveName" width="330" />
				<el-table-column label="本地销毁状态" align="center" prop="localStatus" width="150">
					<template #default="scope">
						<dict-tag :options="destroyed_status" :value="scope.row.localStatus" />
					</template>
				</el-table-column>
				<el-table-column label="下发销毁状态" align="center" prop="desStatus" width="150">
					<template #default="scope">
						<dict-tag :options="destroyed_status" :value="scope.row.desStatus" />
					</template>
				</el-table-column>
				<el-table-column label="销户时间" align="center" prop="canceTime" width="330" />
				<el-table-column label="档案类型" align="center" prop="archiveType" width="150">
					<template #default="scope">
						<dict-tag :options="archive_type" :value="scope.row.archiveType" />
					</template>
				</el-table-column>
				<el-table-column label="档案密级" align="center" prop="conLevel" width="100">
					<template #default="scope">
						<dict-tag :options="confd_lv" :value="scope.row.conLevel" />
					</template>
				</el-table-column>
				<el-table-column label="保管期限" align="center" prop="saveDate" width="100">
					<template #default="scope">
						<dict-tag :options="keep_dur" :value="scope.row.saveDate" />
					</template>
				</el-table-column>
				<el-table-column label="资料种类	" align="center" prop="meansType" width="150">
					<template #default="scope">
						<dict-tag :options="means_type" :value="scope.row.meansType" />
					</template>
				</el-table-column>
				<el-table-column label="介质类型" align="center" prop="medForm">
					<template #default="scope">
						<dict-tag :options="med_form" :value="scope.row.medForm" />
					</template>
				</el-table-column>
				<el-table-column label="对象类型" align="center" prop="archObjType" width="150">
					<template #default="scope">
						<dict-tag :options="arch_obj_type" :value="scope.row.archObjType" />
					</template>
				</el-table-column>
				<el-table-column label="对象编号" align="center" prop="archObjNo" width="150" />
				<el-table-column label="对象名称" align="center" prop="archObjName" />
				<el-table-column label="操作" align="center" fixed="right">
					<template #default="scope">
						<el-link type="danger" plain icon="delete" @click="destroyArchives(scope.row)">销毁</el-link>
					</template>
				</el-table-column>
			</el-table>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="close">关 闭</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="ArchiveDesTask">
	import {
		listArchiveDesTask,
		getArchiveDesTask,
		delArchiveDesTask,
		addArchiveDesTask,
		updateArchiveDesTask,
		selectArchiveDesByTaskId,
		destructionFiles,
		report
	} from "@/api/archive/ArchiveDesTask";
	import {
		getCurrentInstance,
		reactive,
		ref,
		toRefs
	} from "vue";
	import modal from "@/plugins/modal";
	const {
		proxy
	} = getCurrentInstance();

	const ArchiveDesTaskList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const openscan = ref(false);
	const archiveDestoryList = ref([]);
	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			roomNo: null,
			desNo: null,
			desName: null,
			desAccomBy: null,
			desContent: null,
			status: null,
			pushStatus: null
		},
		rules: {}
	});
	const {
		push_status,
		execut_result,
		destroyed_status,
		archive_type,
		save_extended,
		borrow_flag,
		keep_dur,
		confd_lv,
		med_form,
		means_type,
		arch_obj_type
	} = proxy.useDict('push_status', 'execut_result', 'destroyed_status', 'archive_type', 'save_extended', 'borrow_flag',
		'keep_dur', 'confd_lv', 'med_form', 'means_type', 'arch_obj_type');
	const {
		queryParams,
		form,
		rules
	} = toRefs(data);


	let selectFileList = []
	let selectUserList = []
	let handleViewTaskId = null

	/** 查询销毁任务列表 */
	function getList() {
		loading.value = true;
		listArchiveDesTask(queryParams.value).then(response => {
			ArchiveDesTaskList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}
	// 关闭按钮
	function close() {
		openscan.value = false;
		getList();
	}

	// 表单重置
	function reset() {
		form.value = {
			taskId: null,
			orgNo: null,
			roomNo: null,
			desNo: null,
			desName: null,
			desAccomBy: null,
			desContent: null,
			pushStatus: "0",
			pushTime: null,
			createTime: null,
			creator: null,
			modifyTime: null,
			modifier: null,
			status: "0"
		};
		proxy.resetForm("ArchiveDesTaskRef");
		archiveDestoryList.value = [];
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.taskId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加销毁任务";
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const taskIds = row.taskId || ids.value;
		proxy.$modal.confirm('是否确认删除销毁任务编号为"' + taskIds + '"的数据项？').then(function() {
			return delArchiveDesTask(taskIds);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('archive/ArchiveDesTask/export', {
			...queryParams.value
		}, `档案销毁_${new Date().getTime()}.xlsx`)
	}

	function handleView(row) {
		openscan.value = true;
		handleViewTaskId = row.taskId;
		getArchiveDesTask(handleViewTaskId).then(response => {
			form.value = response.data;
		});
		selectArchiveDesByTaskId(handleViewTaskId).then(response => {
			archiveDestoryList.value = response.data;
		});
	}

	function submitReport(row, index) {
		reportButton(row, index);
	}

	/**
	 * 销毁
	 * @param row
	 */
	function destroyArchives(row) {
		if (!(selectFileList.length && selectFileList.indexOf(row) !== -1)) {
			proxy.$refs.destructionFiles.toggleRowSelection(row)
		}
		destroyArchivesList();
		getList();
	}

	function destroyArchivesList() {
		const taskId = form.value.taskId;
		let ids = []
		let msg = ""
		let formData = new FormData();
		selectFileList.forEach(item => {
			if (item.localStatus === "03") {
				ids.push(item.id)
			} else if (item.localStatus === "01") {
				msg += "档案名称" + item.archiveName + "状态为【销毁】;"
			} else {
				msg += "档案名称" + item.archiveName + "状态为【留存】;"
			}
		})

		if (msg != '') {
			modal.msgError(msg + "请您重新选择！");
			return;
		}

		if (ids.length == 0) {
			modal.msgWarning("请选择需要销毁的档案！");
			return
		}
		formData.append("ids", ids)
		formData.append("localStatus", "01")
		formData.append("taskId", taskId)
		destructionFiles(formData).then(response => {
			modal.msgSuccess("销毁档案成功");
			selectArchiveDesByTaskId(handleViewTaskId).then(response => {
				archiveDestoryList.value = response.data;
			});
		}).catch(error => {
			modal.msgError(error);
		})
	}

	function handleSelectionChangeArchives(val) {
		//console.log(val)
		selectFileList = val;
	}

	function handleSelectionChangeUser(val) {
		selectUserList = val;
	}

	getList();
</script>
